package com.se.mapper;

import com.se.domain.mysql.ApplyDO;
import com.se.domain.qo.ApplyActQO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface ApplyMapper {
    @Select("select count(*) from apply where qn_id = #{qnId} and active = true")
    boolean existByQnId(@Param("qnId") Long qnId);

    @Select("select count(*) from apply where qn_id = #{qnId} and user_id = #{userId} and active = true")
    boolean existByQnIdAndUserId(@Param("qnId") Long qnId, @Param("userId") Long userId);
    @Select("select apply_id from apply where qn_id = #{qnId} and user_id = #{userId} and active = true")
    Long getApplyIdByQnIdAndUserId(@Param("qnId") Long qnId, @Param("userId") Long userId);
    @Select("select * from apply where qn_id = #{qnId} and user_id = #{userId} and active = true")
    ApplyDO getApplyByQnIdAndUserId(@Param("qnId") Long qnId, @Param("userId") Long userId);
    @Insert("insert into apply(user_id, act_id, qn_id) " +
            "values (#{userId}, #{actId}, #{qnId}) " +
            "on duplicate key update active = true")
    @Options(useGeneratedKeys = true, keyProperty = "applyId", keyColumn = "apply_id")
    void insert(ApplyDO applyDO);

    @Select("select * from apply where apply_id = #{applyId} and active = true")
    ApplyDO getByApplyId(@Param("applyId") Long applyId);

    @Update("update apply set active = false where apply_id = #{applyId} and active = true")
    void logicDeleteByApplyId(@Param("applyId") Long applyId);

    @Update("update apply set sign = #{sign} where apply_id = #{applyId} and active = true")
    void updateSignByApplyId(@Param("applyId") Long applyId, @Param("sign") boolean sign);


    @Select("select * from apply where qn_id = #{qnId} and active = true " +
            "limit #{l1}, #{l2}")
    List<ApplyDO> listByAnId(@Param("qnId") Long qnId, @Param("l1") int l1, @Param("l2") int l2);


    @Select("select user_id from apply where qn_id = #{qnId} and active = true")
    List<Long> listUserIdByQnId(@Param("qnId") Long qnId);

    @Select("select * from apply where qn_id = #{qnId} and active = true")
    List<ApplyDO> listByQnId(@Param("qnId") Long qnId);
}
